package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLTranPlanRdcToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLTranPlanRdcToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 调拨计划单RDC fc到f4
 *
 * @author 康聪
 * @date 2018/11/27
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLTranPlanRdcToF4QueryCmdImpl extends CommandAdapter implements ViewDLTranPlanRdcToF4QueryCmd {

    private final static FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        log.info("ViewDLTranPlanRdcToF4QueryCmdImpl--map-->{}",map);
//        log.debug("-----------ViewDLTranPlanRdcToF4QueryCmdImpl---------------" + map.toString());
        ValueHolder vh = new ValueHolder();
        ViewDLTranPlanRdcToF4Mapper viewDLTranPlanRdcToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLTranPlanRdcToF4Mapper.class);
        JSONObject param = null;
        try {
            param = JSON.parseObject(map.get("param").toString());
        } catch (Exception e) {
            log.error("ViewDLTranPlanRdcToF4QueryCmdImpl--error-->" + map.toString());
            throw new NDSException("参数为：" + map.toString() + "，参数转换异常：" + e);
        }

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();

        if ("submit".equals(action)) {
            //提交
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.selTranPlanRdcData(ids);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONArray itemJsonArray = new JSONArray();
                JSONObject terJson = new JSONObject(hashMap);
                Long id = terJson.getLong("ID");

                List<HashMap> itemList = viewDLTranPlanRdcToF4Mapper.findTranPlanItemRdcData(id);
                for (HashMap itemHashMap : itemList) {
                    JSONObject itemJson = new JSONObject(itemHashMap);
                    itemJsonArray.add(itemJson);
                }

                terJson.remove("ID");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                param1.put("DL_T_RDC_TRAN_ITEM_TRANSFER", itemJsonArray);
                jsonArray.add(param1);
            }
        } else if ("unsubmit".equals(action)) {
            //取消提交
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.selTranPlanRdcData1(ids);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONObject terJson = new JSONObject(hashMap);
                terJson.remove("ID");
                terJson.put("BILL_TYPE", "RATION_RDC");
                terJson.put("STATUS_TYPE", "UNCHK");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("terminat".equals(action)) {
            //终止
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.selTranPlanRdcData1(ids);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONObject terJson = new JSONObject(hashMap);
                terJson.remove("ID");
                terJson.put("BILL_TYPE", "RATION_RDC");
                terJson.put("STATUS_TYPE", "TERMINAT");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("unterminat".equals(action)) {
            //取消终止
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.selTranPlanRdcData1(ids);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONObject terJson = new JSONObject(hashMap);
                terJson.remove("ID");
                terJson.put("BILL_TYPE", "RATION_RDC");
                terJson.put("STATUS_TYPE", "UNTERMINAT");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("void".equals(action)) {
            //作废
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.selTranPlanRdcData1(ids);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONObject terJson = new JSONObject(hashMap);
                terJson.remove("ID");
                terJson.put("BILL_TYPE", "RATION_RDC");
                terJson.put("STATUS_TYPE", "DEL");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("execute".equals(action)) {
            //执行
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.selTranPlanRdcData1(ids);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONArray itemJsonArray = new JSONArray();
                JSONObject terJson = new JSONObject(hashMap);
                Long id = terJson.getLong("ID");

                List<HashMap> itemList = viewDLTranPlanRdcToF4Mapper.findTranPlanItemRdcData1(id);
                for (HashMap itemHashMap : itemList) {
                    JSONObject itemJson = new JSONObject(itemHashMap);
                    itemJsonArray.add(itemJson);
                }

                terJson.remove("ID");
                terJson.put("BILL_TYPE", "RATION_RDC");
                terJson.put("STATUS_TYPE", "SEND");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                param1.put("DL_T_BILL_STATUS_CHANGE_ITEM", itemJsonArray);
                jsonArray.add(param1);
            }
        } else if ("TRAN_SEND".equals(action)) {
//            log.debug("-----------ViewDLTranPlanRdcToF4QueryCmdImpl----------TRAN_SEND--" + ids);
            //调拨出库单发出
            List<HashMap> list1 = viewDLTranPlanRdcToF4Mapper.selTranPlanNoRdc(ids);
            Object[] billnos = listToJsonarray(list1).toArray();
            List<HashMap> list = viewDLTranPlanRdcToF4Mapper.findTranPlanRdcData2(billnos);

            for (HashMap hashMap : list) {
                if (hashMap == null || hashMap.size() == 0) {
                    continue;
                }
                JSONArray itemJsonArray = new JSONArray();
                JSONObject terJson = new JSONObject(hashMap);
                Long id = terJson.getLong("ID");

                List<HashMap> itemList = viewDLTranPlanRdcToF4Mapper.findTranPlanItemRdcData1(id);
                for (HashMap itemHashMap : itemList) {
                    JSONObject itemJson = new JSONObject(itemHashMap);
                    itemJsonArray.add(itemJson);
                }

                terJson.remove("ID");
                terJson.put("BILL_TYPE", "RATION_RDC");
                terJson.put("STATUS_TYPE", "TRAN_SEND");
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                param1.put("DL_T_BILL_STATUS_CHANGE_ITEM", itemJsonArray);
                jsonArray.add(param1);
            }
        }

        vh.put("code", 1);
        vh.put("data", jsonArray);
        return vh;
    }

    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = mapList.get(i);
            if (map == null) {
                continue;
            }
            JSONObject jsonObject = new JSONObject(map);
            String bill_no = jsonObject.getString("FROM_BILL_NO");
            jsonArray.add(bill_no);
        }
        return jsonArray;
    }


}
